import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;


@WebServlet("/servlet")
public class Servlet02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet.........get.........");
        req.setCharacterEncoding("utf-8");
        String parameterString = req.getQueryString();
        System.out.println(parameterString);
        String[] parametersArr = parameterString.split("&");
        String username = parametersArr[0].split("=")[1];
        String password = parametersArr[1].split("=")[1];
        String gander = parametersArr[2].split("=")[1];
        String[] hobbies = req.getParameterValues("hobby");

        System.out.println(username);
        System.out.println(password);
        System.out.println(gander);
        System.out.println(Arrays.toString(hobbies));

        // for (int i = 0; i <parametersArr.length ; i++) {
        //     System.out.println(Arrays.toString(parametersArr));
        //     System.out.println(parametersArr[i]);
        //     String[] parameterArr = parameterString.split("=");
        //     for (int j = 0; j <parameterArr.length ; j++) {
        //         System.out.println(parameterArr[j]);

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("servlet.........post........");
        req.setCharacterEncoding("utf-8");
        BufferedReader reader = req.getReader();
        String line;
        while ((line = reader.readLine())!= null){
            System.out.println(line);
            String[] parametersArr = line.split("&");
            String username = parametersArr[0].split("=")[1];
            String password = parametersArr[1].split("=")[1];
            String gander = parametersArr[2].split("=")[1];
            String[] hobbies = req.getParameterValues("hobby");

            System.out.println(username);
            System.out.println(password);
            System.out.println(gander);
            System.out.println(Arrays.toString(hobbies));

        }
    }
}
